/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
 var mergeTrees = function(root1, root2) {
    if(!root1&&!root2){
        return null;
    }
    else if(!root1){
        // return new TreeNode(root2.val,mergeTrees(null,root2.left),mergeTrees(null,root2.right));
        return root2;  // 树1该节点往下都是空，直接用树2的该节点代替
    } 
    else if(!root2){
        // return new TreeNode(root1.val,mergeTrees(root1.left,null),mergeTrees(root1.right,null));
        return root1;  // 树2该节点往下都是空，直接用树1的该节点代替
    }
    else{
        return new TreeNode(root1.val+root2.val,mergeTrees(root1.left,root2.left),mergeTrees(root1.right,root2.right));
    }
}; 
 var mergeTrees = function(root1, root2) {
    if(!root1&&!root2){
        return null;
    }
    else if(!root1 && root2){
        let newnode=new TreeNode(root2.val);
        newnode.left=mergeTrees(null,root2.left);
        newnode.right=mergeTrees(null,root2.right);
        return newnode;
    }
    else if(root1 && !root2){
        let newnode=new TreeNode(root1.val);
        newnode.left=mergeTrees(root1.left,null);
        newnode.right=mergeTrees(root1.right,null);
        return newnode;
    }
    else{
        let newnode=new TreeNode(root1.val+root2.val);
        newnode.left=mergeTrees(root1.left,root2.left);
        newnode.right=mergeTrees(root1.right,root2.right);
        return newnode;
    }
};